<?php

class m_penyusutan_detail extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_penyusutan_by_barang_id_penyusutan_tgl_awal_akhir($params) {
        $sql = "SELECT * FROM penyusutan_detail WHERE barang_id = ? AND penyusutandet_tgl_awal = ? AND penyusutandet_tgl_akhir = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_penyusutan_by_penyusutan_id($params) {
        $sql = "SELECT * FROM penyusutan_detail WHERE penyusutan_id = ? ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    //penyusutan detail dengan pagination
    function get_count_all_penyusutan_detail_by_penyusutandet_id($params) {
        $sql = "SELECT COUNT(*)'total' FROM penyusutan_detail WHERE penyusutan_id = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    function get_all_penyusutan_detail_by_penyusutandet_id_pagination($params) {
        $sql = "SELECT a.*, b.barang_nama FROM penyusutan_detail a
                JOIN barang b ON a.barang_id = b.barang_id
                WHERE a.penyusutan_id = ?
                ORDER BY a.penyusutandet_id DESC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO penyusutan_detail VALUES (NULL, ?, ?, ?, ?, ?, ?, ?,  NOW())";
        return $this->db->query($sql, $user_id);
    }

}
